import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

import Layout from '@/layouts';
import novelRouter from './modules/novel';
import analyticsRouter from './modules/analytics';

//所有路由配置
export const baseRoute = [
  {
    path: '/',
    redirect: '/novel/list',
    hidden: true
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/403',
    component: () => import('@/views/error/403'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/500',
    component: () => import('@/views/error/500'),
    hidden: true
  },
  novelRouter,
  analyticsRouter,
  {
    name: 'userSystem',
    component: Layout,
    path: '/userSystem',
    redirect: '/userSystem/userInfo',
    meta: { title: '个人设置', icon: 'user', role: 11 },
    children: [
      {
        name: 'userInfo',
        path: 'userInfo',
        component: () => import('@/views/userSystem/userInfo/index'),
        meta: { title: '个人中心', role: 12 }
      },
      {
        name: 'setting',
        path: 'setting',
        component: () => import('@/views/userSystem/setting/index'),
        meta: { title: '个人设置', role: 13 }
      }
    ]
  },
  {
    name: 'system',
    component: Layout,
    path: '/system',
    redirect: '/system/userManage',
    meta: {
      role: 14,
      title: '系统管理',
      icon: 'system'
    },
    children: [
      {
        name: 'userManage',
        path: 'userManage',
        component: () => import('@/views/system/userManage/index'),
        meta: { title: '用户管理', role: 15 }
      },
      {
        name: 'roleManage',
        path: 'roleManage',
        component: () => import('@/views/system/roleManage/index'),
        meta: { title: '角色管理', role: 16 }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
];

const createRouter = function() {
  return new VueRouter({
    routes: baseRoute,
    scrollBehavior: () => ({ y: 0 })
  });
};

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher;
}

//重定向时报错，用这个不让他报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};

export default router;
